/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tubes1sister.tools;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import tubes1sister.Peer;
import tubes1sister.metainfo;

/**
 *
 * @author Muhammad Anis
 */
public class DirectorySearchHelper {
    //Root Directory
    public String rootDirectory="";

    //konstruktor
    public DirectorySearchHelper(String root){
        this.rootDirectory = root;
    }

    public String isHasFile(String hashFilename){
        File directory = new File(rootDirectory);
        if(directory.isDirectory()){
            //list filenya
            String[] listname = directory.list();
            List<String> hasil = Arrays.asList(listname);
            for(String file : listname){
                try {
                    
                    String s = SHA1Utility.getSHA1(file);
                    String temp=file;
                    file= rootDirectory+"\\"+file;
                    File f = new File(file);
                    if (f.isFile()&&s.equals(hashFilename)) {
                       
                        //cari file torrent yang memiliki sha sama dengan ini
                        if(hasil.contains(temp+".torrent")){
                            return file;
                        }
                    }
                } catch (NoSuchAlgorithmException ex) {
                    Logger.getLogger(DirectorySearchHelper.class.getName()).log(Level.SEVERE, null, ex);
                    return null;
                } catch (UnsupportedEncodingException ex) {
                    Logger.getLogger(DirectorySearchHelper.class.getName()).log(Level.SEVERE, null, ex);
                    return null;
                }
            }
        }
        return null;
    }

    public byte[] getBitField(String filename,String hashvalue,int pieceLength, int indexpiece) throws IOException, NoSuchAlgorithmException{
        //Peer peer = new Peer();
        byte[] temp = Peer.getBytesFromFile(new File(filename));
        byte[] hasil = new byte[pieceLength];

        if (temp.length > indexpiece*pieceLength) {
            int j = 0;
            if ((indexpiece+1)*pieceLength < temp.length)
                hasil = getParseByte(temp, indexpiece*pieceLength, (indexpiece+1)*pieceLength);
            else
                hasil = getParseByte(temp, indexpiece*pieceLength, temp.length);
            String hasilhash = SHA1Utility.getSHA1(SHA1Utility.convertToHex(hasil));
            if (hasilhash.equals(hashvalue)) {
                return hasil;
            }
            else
                return null;
        }
        else {
            return  null;
        }
    }
    
     public  byte[] getParseByte (byte[] bytes, int indeksAwal, int indeksAkhir){
       byte[] temp = new byte[indeksAkhir-indeksAwal];
       int i = 0;
       for(int j=indeksAwal; j<indeksAkhir; j++)
       {
           temp[i] = bytes[j];
           i++;
       }
       return temp;
    }
}
